<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="110px"
    >
      <el-form-item label="转项产品编号" prop="transferltemNo">
        <el-input
          v-model="queryParams.transferltemNo"
          placeholder="请输入转项产品编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="转项产品名称" prop="transferItemProductName">
        <el-input
          v-model="queryParams.transferItemProductName"
          placeholder="请输入转项产品名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="转项人" prop="transferItemManager">
        <el-input
          v-model="queryParams.transferItemManager"
          placeholder="请输入转项人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="转项时间" prop="transferItemTime">
        <el-date-picker
          v-model="queryParams.transferItemTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" type="primary">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <div class="btnWrap">
      <div>
        <el-button
          type="primary"
          @click="handleExport"
          v-hasPermi="['order:transfer-item-management:export']"
        >
          数据导出
        </el-button>
        <el-button
          type="primary"
          @click="openForm('create')"
          v-hasPermi="['order:transfer-item-management:create']"
        >
          新增
        </el-button>
      </div>
      <div>
        <el-button type="primary" @click="openTableTitleSet"> 表头设置 </el-button>
      </div>
    </div>
    <!-- 表单 -->
    <Table
      :columns="tableTitleList"
      :selection="tableObject.tableList.length ? true : false"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      @register="tableoperation"
      border
      :tableHeight="tableHeight"
    >
      <template #action="{ row }">
        <el-button
          link
          type="primary"
          @click="openForm('update', row.transferItemNo)"
          v-if="row.status === 10"
          v-hasPermi="['order:transfer-item-management:update']"
        >
          编辑
        </el-button>
        <el-button
          link
          type="primary"
          @click="openForm('view', row.transferItemNo)"
          v-hasPermi="['order:transfer-item-management:view']"
        >
          查看
        </el-button>
      </template>
    </Table>
  </ContentWrap>

  <!-- 表头设置 -->
  <table-width-set ref="tableTitleSet" @refresh="getTableTitle" />
  <!-- 导出 -->
  <ExportFile ref="exportRef" @download="downloadFun" />
</template>

<script setup lang="ts" name="TransferItemManagement">
   import download from '@/utils/download'
   import * as TransferItemManagementApi from '@/api/orderManagement/transferManagement/transferItemManagement'
   import type { CrudSchema } from '@/hooks/web/useCrudSchemas'

   

   const tableTitleList = ref<CrudSchema[]>([]) // 表头列表
   const code = 'TransferItemManagement' //表头名
   const tableHeight = ref(0)
   const queryParams = reactive({
     pageNo: 1,
     pageSize: 10,
     transferltemNo: null,
     transferItemProductName: null,
     transferItemManager: null,
     transferItemTime: null
   })
   const queryFormRef = ref() // 搜索的表单

   const orderDate = ref([]) //订单日期
   const deliveryDate = ref([]) //发货日期

   // tableObject：表格的属性对象，可获得分页大小、条数等属性
   // tableMethods：表格的操作对象，可进行获得分页、删除记录等操作
   const { tableObject, tableMethods } = useTable({
     getListApi: TransferItemManagementApi.getTransferItemManagementPage, // 分页接口
     defaultParams: queryParams
   })

   /**表头内部方法 */
   const usTableoperation = ref()
   const tableoperation = (table) => {
     usTableoperation.value = table
   }

   //清空多选
   const clearSelectData = () => {
     usTableoperation.value.selections = []
     usTableoperation.value.clearTableSelect()
   }

   // 获得表格的各种操作
   const { getList, setSearchParams } = tableMethods

   // 获取表头
   const getTableTitle = async () => {
     const res = await TransferItemManagementApi.getTransferItemManagementPage(queryParams)
     res.labels.push({
       field: 'action',
       label: '操作',
       align: 'left',
       headerAlign: 'left',
       width: 110
     })
     const { allSchemas } = useCrudSchemas(res.labels)
     tableTitleList.value = allSchemas.tableColumns
   }

   // 查看编辑
   const { push } = useRouter()
   const openForm = (type: string, transferItemNo: string) => {
     push({
       path: '/orderManagement/orderManagement/transferManagement/TransferItemManagementForm',
       query: {
         type,
         transferItemNo
       }
     })
   }

   // 打开表头设置
   const tableTitleSet = ref()
   const openTableTitleSet = () => {
     tableTitleSet.value.open(tableTitleList.value, code)
   }

   /** 搜索按钮操作 */
   const handleQuery = () => {
     queryParams.pageNo = 1
     setSearchParams(queryParams)
   }

   /** 重置按钮操作 */
   const resetQuery = () => {
     queryFormRef.value.resetFields()
     orderDate.value = []
     deliveryDate.value = []
     handleQuery()
   }

   /** 导出按钮操作 */
   const exportRef = ref()
   const handleExport = () => {
     exportRef.value.open()
   }

   // 下载
   const downloadFun = async () => {
     const val = exportRef.value.params
     const params = {
       fileName: val.name,
       ids: ''
     }
     if (val.range === 'all') {
       params.ids = ''
     } else {
       // params.ids = usTableoperation.value.selections.map(item => item.id)
       console.log('usTableoperation.value.selections', usTableoperation.value.selections)
       params.ids = []
       usTableoperation.value.selections.forEach((item) => {
         params.ids.push(item.id)
       })
     }
     console.log(params, 'params')
     TransferItemManagementApi.exportTransferItemManagement(params).then((res) => {
       download.excel(res, val.name)
       clearSelectData()
     })
   }

   /** 初始化 **/
   onMounted(async () => {
     const queryParamsHeight = queryFormRef.value.$el.clientHeight + 2
     tableHeight.value = document.body.clientHeight - 132 - queryParamsHeight - 117
     getTableTitle()
     getList()
   })
</script>
<style lang="scss" scoped>
   .btnWrap {
     display: flex;
     justify-content: space-between;
     margin-bottom: 10px;
   }
</style>
